package com.qqtech.qquan.order.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.qquan.mall.enums.MallSupplierStatusDictEnum;
import com.qqtech.qquan.mall.model.MallSupplier;
import com.qqtech.qquan.mall.model.MallSupplierQuery;
import com.qqtech.qquan.mall.service.MallSupplierService;
import com.qqtech.qquan.order.model.OrderItemQuery;
import com.qqtech.qquan.order.model.OrderItemVo;
import com.qqtech.qquan.order.service.OrderBaseService;
import com.qqtech.qquan.order.service.OrderItemService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.sys.enums.SysConst;
import com.qqtech.qquan.sys.model.SysParam;
import com.qqtech.qquan.sys.service.SysParamService;
import com.qqtech.qquan.util.ExcelUtil;


@Controller
@RequestMapping(value = "/order/item")
public class OrderItemController extends BaseAdminController {

	@Resource
	private OrderBaseService orderBaseService;

	@Resource
	private OrderItemService orderItemService;

	@Resource
	private MallSupplierService mallSupplierService;
	@Resource
	private SysParamService sysParamService;
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		MallSupplierQuery supplierQuery = new MallSupplierQuery();
		supplierQuery.setStatus(MallSupplierStatusDictEnum.NORMAL.getCode());
		supplierQuery.setIsShow(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.ASC, "id"));
		List<MallSupplier> mallSupplierList = mallSupplierService.queryList(supplierQuery, pageKit);
		model.addAttribute("mallSupplierList", mallSupplierList);
		return "/order/orderitem-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(OrderItemQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "orderNo"));
		Page<OrderItemVo> page = orderItemService.queryPageList4Admin(query, pageKit);
		List<OrderItemVo> orderItemList = page.getContent();
		if(orderItemList!=null && orderItemList.size()>0){
			MallSupplier supplier = null;//供应商
			for(OrderItemVo item : orderItemList){
				if(item.getSupplierId()!=null){
					supplier = mallSupplierService.getById(item.getSupplierId());
					if(supplier!=null){
						item.setSupplierName(supplier.getName());//供应商名称
					}
				}
			}
		}
		adminPage.setRows(orderItemList);
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	@RequestMapping(value = "/export2Excel")
	@ResponseBody
	public Result export2Excel(OrderItemQuery query, HttpServletRequest request, HttpServletResponse response) {
		Result ret = new Result();
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.DESC, "orderNo"));
		int count = orderItemService.queryCount4Admin(query);
		SysParam sysParam = sysParamService.getByKey(SysConst.PARAM_SYS_EXPORT_TOEXCEL_MAXROWS);
		if (sysParam != null && StringUtil.isNotBlank(sysParam.getValue())) {
			int maxExportRows = Integer.valueOf(sysParam.getValue());
			if(count > maxExportRows){
				ret.addErr("导出数量不能超过"+maxExportRows+"行!");
				return ret;
			}
		}
		List<OrderItemVo> orderItemList =  orderItemService.queryList4Admin(query, pageKit);//不分页
		if(orderItemList!=null && orderItemList.size()>0){
			MallSupplier supplier = null;//供应商
			for(OrderItemVo item : orderItemList){
				if(item.getSupplierId()!=null){
					supplier = mallSupplierService.getById(item.getSupplierId());
					if(supplier!=null){
						item.setSupplierName(supplier.getName());//供应商名称
					}
				}
			}
		}
		List<Map<String,Object>> list=createExcelRecord(orderItemList);
    	String columnNames[]={"订单号","产品或服务名称","产品或服务价格","数量","供应商","是否外部供应商订单","下单时间","社区名称"};//列名
        String keys[]   =    {"orderNo","name","price","quntity","supplierName","isExternal","strNewAt","communityName"};//map中的key
        String fileName="订单商品表";
        ExcelUtil.ExcelSingleOutputStream(list,keys,columnNames,fileName,request,response);
        return ret;
	}
	
    private List<Map<String, Object>> createExcelRecord(List<OrderItemVo> orderList) {
        List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sheetName", "订单商品表");
        listmap.add(map);
        OrderItemVo orderItem=null;
        for (int j = 0; j < orderList.size(); j++) {
        	orderItem=orderList.get(j);
            Map<String, Object> mapValue = new HashMap<String, Object>();
            mapValue.put("orderNo", orderItem.getOrderNo());
            mapValue.put("name", orderItem.getName());
            mapValue.put("price", orderItem.getPrice());
            mapValue.put("quntity", orderItem.getQuntity());
            mapValue.put("supplierName", orderItem.getSupplierName());
            mapValue.put("isExternal", orderItem.getIsExternal()==null ? "" : (orderItem.getIsExternal()==1 ? "是" : "否"));
            mapValue.put("strNewAt", orderItem.getStrNewAt());
            mapValue.put("communityName", orderItem.getCommunityName());
            listmap.add(mapValue);
        }
        return listmap;
    }
    
}
